package com.atguigu.dao;

import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

public abstract class AbstractSimpleDao extends NamedParameterJdbcDaoSupport {
	

	/**
	 * 
	 * 主要功能: 注入DataSource<br>
	 * 注意事项: 使用Autowired注解自动注入<br>
	 * 
	 * @param dataSource 数据源bean, 配置到配置文件中
	 * 
	 */
	@Autowired
	public void injiectDataSource(DataSource dataSource) {
		this.setDataSource(dataSource);
		

	}

	/**
	 * 方法 pagingFind
	 * 
	 * @param sql
	 * @param start
	 * @param limit
	 * @param RowMapper <X> rowMapper
	 * @return 按条件分页查询返回PageInfo (分页内的数据对象List 和 总数据数)
	 */
	public abstract <X> Page<X> pagingFind(String sql, long start, long limit, RowMapper<X> rowMapper);

	/**
	 * 方法 pagingFind
	 * 
	 * @param sql
	 * @param start
	 * @param limit
	 * @param RowMapper <X> rowMapper
	 * @param obj
	 * @return 按条件分页查询返回PageInfo (分页内的数据对象List 和 总数据数)
	 */
	public abstract <X> Page<X> pagingFind(String sql, long start, long limit, RowMapper<X> rowMapper, Object obj);

	/**
	 * 方法 pagingFind
	 * 
	 * @param sql
	 * @param start
	 * @param limit
	 * @param RowMapper <X> rowMapper
	 * @param obj
	 * @return 按条件分页查询返回PageInfo (分页内的数据对象List 和 总数据数)
	 */
	public abstract <X> Page<X> pagingFind(String sql, long start, long limit, RowMapper<X> rowMapper, Object[] obj);

	/**
	 * 方法 pagingFind
	 * 
	 * @param sql
	 * @param start
	 * @param limit
	 * @param RowMapper <X> rowMapper
	 * @param Map       <String,Object> map
	 * @return 按条件分页查询返回PageInfo (分页内的数据对象List 和 总数据数)
	 */
	public abstract <X> Page<X> pagingFind(String sql, long start, long limit, RowMapper<X> rowMapper,
			Map<String, Object> map);

	/**
	 * 方法 pagingFindList
	 * 
	 * @param sql
	 * @param start
	 * @param limit
	 * @param RowMapper <X> rowMapper
	 * @return 条件查询分页查询的记录对象
	 */
	public abstract <X> List<X> pagingFindList(String sql, long start, long limit, RowMapper<X> rowMapper);

	/**
	 * 方法 pagingFindList
	 * 
	 * @param sql
	 * @param start
	 * @param limit
	 * @param RowMapper <X> rowMapper
	 * @param obj
	 * @return 条件查询分页查询的记录对象
	 */
	public abstract <X> List<X> pagingFindList(String sql, long start, long limit, RowMapper<X> rowMapper, Object obj);

	/**
	 * 方法 pagingFindList
	 * 
	 * @param sql
	 * @param start
	 * @param limit
	 * @param RowMapper <X> rowMapper
	 * @param obj
	 * @return 条件查询分页查询的记录对象
	 */
	public abstract <X> List<X> pagingFindList(String sql, long start, long limit, RowMapper<X> rowMapper,
			Object[] obj);

	/**
	 * 方法 pagingFindList
	 * 
	 * @param sql
	 * @param start
	 * @param limit
	 * @param RowMapper <X> rowMapper
	 * @param Map       <String,Object> map
	 * @return 条件查询分页查询的记录对象
	 */
	public abstract <X> List<X> pagingFindList(String sql, long start, long limit, RowMapper<X> rowMapper,
			Map<String, Object> map);

	/**
	 * 方法 pagingFindCount
	 * 
	 * @param sql
	 * @return 条件查询记录总数
	 */
	public abstract long pagingFindCount(String sql);

	/**
	 * 方法 pagingFindCount
	 * 
	 * @param sql
	 * @param obj
	 * @return 条件查询记录总数
	 */
	public abstract long pagingFindCount(String sql, Object obj);

	/**
	 * 方法 pagingFindCount
	 * 
	 * @param sql
	 * @param obj
	 * @return 条件查询记录总数
	 */
	public abstract long pagingFindCount(String sql, Object[] obj);

	/**
	 * 方法 pagingFindCount
	 * 
	 * @param sql
	 * @param Map <String,Object>
	 * @return 条件查询记录总数
	 */
	public abstract long pagingFindCount(String sql, Map<String, Object> map);

	/**
	 * genPkBySeqname
	 * 
	 * @param seqName
	 * @return 取序列的下一个值
	 */
	public abstract String genPkBySeqname(String seqName);

	// //////////////////////////////////////////////////////////
	// 包装 SimpleJdbcTemplate 的方法
	// /////////////////////////////////////////////////////////
	public int queryForInt(String sql, Map<String, ?> args) throws DataAccessException {
		//return super.getJdbcTemplate().queryForObject(sql,Integer.class, args);
		return super.getNamedParameterJdbcTemplate().queryForObject(sql, args,Integer.class);
	}

	public int queryForInt(String sql, SqlParameterSource args) throws DataAccessException {
		return super.getJdbcTemplate().queryForObject(sql,Integer.class, args);
	}

	public int queryForInt(String sql, Object[] args) throws DataAccessException {
		return super.getJdbcTemplate().queryForObject(sql, args,Integer.class);
	}

	public long queryForLong(String sql, Map<String, ?> args) throws DataAccessException {
		//return super.getJdbcTemplate().queryForObject(sql,Integer.class, args);
		return super.getNamedParameterJdbcTemplate().queryForObject(sql, args,Integer.class);
	}

	public long queryForLong(String sql, SqlParameterSource args) throws DataAccessException {
		return super.getJdbcTemplate().queryForObject(sql,Integer.class, args);
	}

	public long queryForLong(String sql, Object[] args) throws DataAccessException {
		return super.getJdbcTemplate().queryForObject(sql, args,Integer.class);
	}

	public <T> T queryForObject(String sql, Class<T> requiredType, Map<String, ?> args) throws DataAccessException {
		//return super.getJdbcTemplate().queryForObject(sql, requiredType, args);
		return super.getNamedParameterJdbcTemplate().queryForObject(sql, args, requiredType);
	}

	public <T> T queryForObject(String sql, Class<T> requiredType, SqlParameterSource args) throws DataAccessException {
		return super.getJdbcTemplate().queryForObject(sql, requiredType, args);
	}

	public <T> T queryForObject(String sql, Class<T> requiredType, Object[] args) throws DataAccessException {
		return super.getJdbcTemplate().queryForObject(sql, requiredType, args);
	}

	public <T> T queryForObject(String sql, RowMapper<T> rm, Map<String, ?> args) throws DataAccessException {
		//return super.getJdbcTemplate().queryForObject(sql, rm, args);
		return super.getNamedParameterJdbcTemplate().queryForObject(sql, args, rm);
	}

	public <T> T queryForObject(String sql, RowMapper<T> rm, SqlParameterSource args) throws DataAccessException {
		return super.getJdbcTemplate().queryForObject(sql, rm, args);
	}
	
	public <T> T queryForObject(String sql, RowMapper<T> rm, Object[] args) throws DataAccessException {
		return super.getJdbcTemplate().queryForObject(sql, rm, args);
	}
	
	public <T> List<T> query(String sql, RowMapper<T> rm, Map<String, ?> args) throws DataAccessException {
		//return super.getJdbcTemplate().query(sql, rm, args);
		return super.getNamedParameterJdbcTemplate().query(sql, args, rm);
	}

	public <T> List<T> query(String sql, RowMapper<T> rm, SqlParameterSource args) throws DataAccessException {
		return super.getJdbcTemplate().query(sql, rm, args);
	}
	
	public <T> List<T> query(String sql, RowMapper<T> rm, Object[] args) throws DataAccessException {
		return super.getJdbcTemplate().query(sql, rm, args);
	}

	public Map<String, Object> queryForMap(String sql, Map<String, ?> args) throws DataAccessException {
		//return super.getJdbcTemplate().queryForMap(sql, args);
		return super.getNamedParameterJdbcTemplate().queryForMap(sql, args);
	}
	
	public Map<String, Object> queryForMap(String sql, SqlParameterSource args) throws DataAccessException {
		return super.getJdbcTemplate().queryForMap(sql, args);
	}
	
	public Map<String, Object> queryForMap(String sql, Object[] args) throws DataAccessException {
		return super.getJdbcTemplate().queryForMap(sql, args);
	}
	
	public List<Map<String, Object>> queryForList(String sql, Map<String, ?> args) throws DataAccessException {
		//return super.getJdbcTemplate().queryForList(sql, args);
		return super.getNamedParameterJdbcTemplate().queryForList(sql, args);
	}
	
	public List<Map<String, Object>> queryForList(String sql, SqlParameterSource args) throws DataAccessException {
		return super.getJdbcTemplate().queryForList(sql, args);
	}
	
	public List<Map<String, Object>> queryForList(String sql, Object[] args) throws DataAccessException {
		return super.getJdbcTemplate().queryForList(sql, args);
	}
	
	public int update(String sql, Map<String, ?> args) throws DataAccessException {
		//return super.getJdbcTemplate().update(sql, args);
		return super.getNamedParameterJdbcTemplate().update(sql, args);
	}
	
	public int update(String sql, SqlParameterSource args) throws DataAccessException {
		return super.getJdbcTemplate().update(sql, args);
	}

	public int update(String sql, Object[] args) throws DataAccessException {
		return super.getJdbcTemplate().update(sql, args);
	}
	
	public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
		return super.getJdbcTemplate().batchUpdate(sql, batchArgs);
	}
	
	public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes) {
		return super.getJdbcTemplate().batchUpdate(sql, batchArgs, argTypes);
	}
	
//	public int[] batchUpdate(String sql, Map<String, ?>[] batchValues) {
//		return super.getJdbcTemplate().batchUpdate(sql, batchValues);
//	}
//	
//	public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
//		return super.getJdbcTemplate().ba(sql, batchArgs);
//	}

}
